// ==UserScript==
// @name         element3ds_question
// @namespace    http://tampermonkey.net/
// @version      1.0.0.0
// @description  try to take over the world!
// @author       laotie
// @match        https://www.element3ds.com/plugin.php?id=ahome_dayquestion:index
// @icon         https://www.google.com/s2/favicons?sz=64&domain=element3ds.com
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==


(function () {
    'use strict';

    let timu = '';
    let options = [];
    let database = {};

    loadData();
    console.log('data:', database);

    findEle('span:nth-child(3) > font', function (ele) {
        timu = ele.innerText; //【题目】 【送分】
        timu = timu.replace('【题目】', '').replace('【送分】', '').replace('【发布】', '').replace(' ', '');
        findAnswer(timu, function (answer) {
            if(answer==undefined){
                answer=0;
            }
            console.log(database,timu,answer,options,'answer');
            findEleAll('div.qs_option', function (ele) {
                ele.forEach(function (item, i) {
                    if (i == answer) {
                        item.click();
                    }
                    options.push(item.innerText);
                });
                setTimeout(() => {
                    findEle('button.pn', function (ele) {
                        ele.click();
                        findResult();
                    });
                },200);
            });
        });
    });

    function findResult() {
        findEle('#messagetext > p:nth-child(1)', function (ele) {
            let result = ele.innerText;
            alert("回答结果:", result);
            if (result.indexOf('正确答案是：') > -1) {
                let option = result.split('正确答案是：')[1];
                database[timu] = options.indexOf(option);
                save('data', database);
            }
        });
    }

    function findAnswer(timu, cb) {
        let data = GM_getValue('data');
        if (data) {
            database = JSON.parse(data);
            let answer = database[timu];
            if (answer != undefined) {
                cb(answer);
            }else{
                database[timu] = 0;
                save('data', database);
                cb(0);
            }
        } else {
            database[timu] = 0;
            save('data', database);
            cb(0);
        }
    }

    function findEle(css, cb) {
        let element = document.querySelector(css);
        if (element) {
            cb(element);
        } else {
            setTimeout(() => {
                findEle(css, cb);
            }, 100);
        }
    }

    function findEleAll(css, cb) {
        let element = document.querySelectorAll(css);
        if (element && element.length > 0) {
            cb(element);
        } else {
            setTimeout(() => {
                findEleAll(css, cb);
            }, 100);
        }
    }

    function save(key, data) {
        GM_setValue(key, JSON.stringify(data));
    }

    function loadData(){
        let data = GM_getValue('data');
        if (data) {
            database = JSON.parse(data);
        } else {
            database = {};
        }
    }
})();
